package com.xeware.chatter.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sforce.soap.partner.sobject.SObject;
import com.sforce.ws.bind.XmlObject;

public final class DFeedComments {
	public List<DFeedComment> comments; 
	public boolean valid = false;
	
	public DFeedComments(SObject s) {
		this.comments = new ArrayList<DFeedComment>();
		XmlObject feedcomments = (XmlObject) s.getChild("FeedComments"); // we should be able to migrate tracked change code to similar pattern...
		if (feedcomments.hasChildren()) {
			Iterator <XmlObject> ckids = feedcomments.getChildren();
			while(ckids.hasNext()) {
				XmlObject ckid = ckids.next();
				if (ckid.getName().getLocalPart().equals("records")) {
					this.processCommentRecord(ckid);
				}
			}
		}
	}
	
	public void processCommentRecord(XmlObject ckid) {
		XmlObject ccreator = ckid.getChild("CreatedBy");
		String cauthor = (String) ccreator.getField("Name");
		String cpost = (String) ckid.getField("CommentBody");
		comments.add(0, new DFeedComment(cauthor, cpost));		
	}
	
	@Override
	public String toString() {
		String mstring = "";
		if (this.comments.size() > 0) {
			for(DFeedComment dfc: this.comments) {
				mstring += dfc.toString() +"\n";
			}
		}
		return mstring;
	}
}

